import json
import asyncio
from traceback import format_exc

from mozi_ai_x.utils.log import mprint_with_name
from vectorvein.settings import settings

from agent import etc
from agent.agent import Agent
from agent.env import Environment

mprint = mprint_with_name(__name__)

with open(".settings.json", "r") as f:
    settings.load(json.load(f))


async def main():
    # 创建环境对象
    env = Environment(
        etc.SERVER_IP,
        etc.SERVER_PORT,
        etc.SCENARIO_NAME,
        etc.SIMULATE_COMPRESSION,
        etc.DURATION_INTERVAL,
        etc.PLATFORM,
    )

    # 启动环境
    await env.start()

    # 重置环境
    await env.reset()

    # 创建智能体对象
    agent = Agent(env)

    # 主循环
    while not env.is_done():
        try:
            await agent.step()
            await env.step()
        except Exception as e:
            mprint.error(f"发生异常: {e}")
            mprint.error(format_exc())


if __name__ == "__main__":
    asyncio.run(main())
